//
//  LoginViewController.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/16.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "LoginViewController.h"
#import "HomeViewController.h"
@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *accountNumberTF;
@property (nonatomic,strong) UITextField *passwordNumberTF;
@end

@implementation LoginViewController

#pragma mark - Life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = MAIN_Color;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginOut) name:@"out" object:nil];
    [self setUI];
    if ([Userdefault getUser][@"accessToken"]) {
        HomeViewController *con = [[HomeViewController alloc]init];
        [self.navigationController pushViewController:con animated:NO];
    }
}
- (void)viewWillAppear:(BOOL)animated{
   
       NSUserDefaults *userde = [NSUserDefaults standardUserDefaults];
       self.accountNumberTF.text = [userde objectForKey:@"phone"];
       self.passwordNumberTF.text = nil;
   
}
- (void)loginOut{
    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"退出成功" animated:YES];
}
#pragma mark - Property

#pragma mark - UI
- (void)setUI{
    UIImageView *logo = [[UIImageView alloc]init];
    logo.image = [UIImage imageNamed:@"zhongjiao"];
    [self.view addSubview:logo];
    [logo mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsPad) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(200*PadHeightRate);
            make.size.mas_equalTo(CGSizeMake(82*PadHeightRate, 86.5*PadHeightRate));
        }else{
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(80*WidthRate);
        make.size.mas_equalTo(CGSizeMake(82*WidthRate, 86.5*WidthRate));
        }
    }];
    
    UILabel *appLab  = [[UILabel alloc]init];
    appLab.textColor = Bai_Color;
    appLab.text = @"中咨巡检通";
    appLab.textAlignment = 1;
    if (IsPad) {
        appLab.font = [UIFont boldSystemFontOfSize:23*PadZitiRate];
    }else{
    appLab.font = [UIFont boldSystemFontOfSize:23*ZitiRate];
    }
    [self.view addSubview:appLab];
    [appLab mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsPad) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(25*WidthRate);
            make.top.mas_equalTo(293*PadHeightRate);
        }else{
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(25*WidthRate);
        make.top.mas_equalTo(175*WidthRate);
        }
    }];
    
    UIView *baiBg = [[UIView alloc]init];
    baiBg.backgroundColor = Bai_Color;
    baiBg.layer.cornerRadius = 5*ZitiRate;
    baiBg.layer.masksToBounds = YES;
    [self.view addSubview:baiBg];
    [baiBg mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsPad) {
            make.centerX.mas_equalTo(0);
            make.width.mas_equalTo(340*PadWidthRate);
            make.height.mas_equalTo(115*PadHeightRate);
            make.top.mas_equalTo(375*PadHeightRate);
        }else{
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(340*WidthRate);
        make.height.mas_equalTo(115*WidthRate);
        make.top.mas_equalTo(252*WidthRate);
        }
    }];
    
    self.accountNumberTF = [[UITextField alloc]init];
    self.accountNumberTF.borderStyle = UITextBorderStyleNone;
    self.accountNumberTF.placeholder  =  @"账号";
    self.accountNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    if (IsPad) {
        self.accountNumberTF.font = [UIFont systemFontOfSize:18*PadZitiRate];
         [self.accountNumberTF setValue:[UIFont systemFontOfSize: 18*PadZitiRate] forKeyPath:@"_placeholderLabel.font"];
    }else{
    self.accountNumberTF.font = [UIFont systemFontOfSize:18*ZitiRate];
         [self.accountNumberTF setValue:[UIFont systemFontOfSize: 18*ZitiRate] forKeyPath:@"_placeholderLabel.font"];
    }
    
    self.accountNumberTF.delegate = self;
    self.accountNumberTF.textColor = Gray_Color;
    self.accountNumberTF.returnKeyType = UIReturnKeyNext;
    [self.accountNumberTF setValue:TiShi_Color forKeyPath:@"_placeholderLabel.textColor"];
    self.accountNumberTF.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    [baiBg addSubview:self.accountNumberTF];
    [self.accountNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsPad) {
            make.left.mas_equalTo(15*PadWidthRate);
            make.right.mas_equalTo(-15*PadWidthRate);
            make.height.mas_equalTo(57*PadHeightRate);
            make.top.mas_equalTo(0);
        }else{
        make.left.mas_equalTo(15*WidthRate);
        make.right.mas_equalTo(-15*WidthRate);
        make.height.mas_equalTo(57*WidthRate);
        make.top.mas_equalTo(0);
        }
    }];
    
    self.passwordNumberTF = [[UITextField alloc]init];
    self.passwordNumberTF.borderStyle = UITextBorderStyleNone;
    self.passwordNumberTF.placeholder  =  @"密码";
    self.passwordNumberTF.returnKeyType = UIReturnKeyDone;
    self.passwordNumberTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (IsPad) {
        self.passwordNumberTF.font = [UIFont systemFontOfSize:18*PadZitiRate];
        [self.passwordNumberTF setValue:[UIFont systemFontOfSize: 18*PadZitiRate] forKeyPath:@"_placeholderLabel.font"];
    }else{
        self.passwordNumberTF.font = [UIFont systemFontOfSize:18*ZitiRate];
        [self.passwordNumberTF setValue:[UIFont systemFontOfSize: 18*ZitiRate] forKeyPath:@"_placeholderLabel.font"];
    }
    self.passwordNumberTF.delegate = self;
    self.passwordNumberTF.textColor = Gray_Color;
    self.passwordNumberTF.secureTextEntry = YES;
    [self.passwordNumberTF setValue:TiShi_Color forKeyPath:@"_placeholderLabel.textColor"];
    self.passwordNumberTF.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
    [baiBg addSubview:self.passwordNumberTF];
    [self.passwordNumberTF mas_makeConstraints:^(MASConstraintMaker *make) {
        if (IsPad) {
            make.left.mas_equalTo(15*PadWidthRate);
            make.right.mas_equalTo(-15*PadWidthRate);
            make.height.mas_equalTo(57*PadHeightRate);
            make.top.mas_equalTo(58*PadHeightRate);
        }else{
        make.left.mas_equalTo(15*WidthRate);
        make.right.mas_equalTo(-15*WidthRate);
        make.height.mas_equalTo(57*WidthRate);
        make.top.mas_equalTo(58*WidthRate);
        }
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = Line_Color;
    [baiBg addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
        make.centerY.mas_equalTo(0);
    }];
   
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [loginBtn setTitle:@"登 录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    loginBtn.backgroundColor = [PublicMethod colorWithHexString:@"#138ce2" andAlpha:1];
    if (IsPad) {
        loginBtn.titleLabel.font = [UIFont systemFontOfSize:20*PadZitiRate];
        loginBtn.layer.cornerRadius = 5;
    }else{
        loginBtn.titleLabel.font = [UIFont systemFontOfSize:20*ZitiRate];
        loginBtn.layer.cornerRadius = 5;
    }
    
    loginBtn.layer.masksToBounds = YES;
    [loginBtn addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(baiBg.mas_left).offset(0);
        make.right.equalTo(baiBg.mas_right).offset(0);
        if (IsPad) {
            make.height.mas_equalTo(56*PadHeightRate);
            make.top.equalTo(baiBg.mas_bottom).offset(25*PadHeightRate);
        }else{
        make.height.mas_equalTo(56*WidthRate);
        make.top.equalTo(baiBg.mas_bottom).offset(25*WidthRate);
        }
    }];
}
#pragma mark - UI Response
- (void)login{

    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool goToLoginWithLoginId:self.accountNumberTF.text userPwd:self.passwordNumberTF.text block:^(id data) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
        if (data) {
        
            if ([data[@"success"] integerValue]) {
                
                [[NSUserDefaults standardUserDefaults] setObject:self.accountNumberTF.text forKey:@"phone"];
                [[NSUserDefaults standardUserDefaults]synchronize];
                [Userdefault setUser:data[@"data"]];
                UIViewController *VC;
                for (UIViewController *controller in self.navigationController.viewControllers) {
                    if ([controller isKindOfClass:NSClassFromString(@"HomeViewController")]) {
                        
                        VC=controller;
                        
                    }
                    
                }
                
                if (VC) {
                    [self.navigationController popToViewController:VC animated:YES];
                }else{
                    HomeViewController*homeVC = [HomeViewController new];
                    [self.navigationController pushViewController:homeVC animated:YES];
                }
            }else{
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
#pragma mark - Data handle

#pragma mark - Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isEqual:self.accountNumberTF]) {
        [self.passwordNumberTF becomeFirstResponder];
    }else{
        [self login];
    }
    return YES;
}

@end
